<?php defined('LFNC_VERSION') or die('forbidden');


/**
* MLT Web Engine
* http://www.molotsoft.ru
*
* @package MLT_WE
* @subpackage Lucky_Funcs
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: string.lfnc.php 141 2009-03-02 17:56:50Z molotsoft@gmail.com $
* @link $HeadURL: http://lucky-funcs.googlecode.com/svn/trunk/funcs/string.lfnc.php $
*/


define('CR', "\r");
define('LF', "\n");
define('CRLF', CR . LF);
define('NL', CRLF);


function hsc_decode($string) {
	return html_entity_decode($string, ENT_QUOTES); // php 4 >= 4.3.0
}


function hsc($string) {
	return php_version_ge('5.2.3') ? htmlspecialchars($string, ENT_QUOTES, '', FALSE) : htmlspecialchars(hsc_decode($string), ENT_QUOTES);
}

function hsc_deep($value) {
	return is_array($value) ? array_map('hsc_deep', $value) : hsc($value);
}

function vsprintf_hsc($format, $args) {
	return vsprintf($format, hsc_deep($args));
}

function sprintf_hsc($format /* [, args [, ...]] */ ) {
	$args = func_get_args();
	return vsprintf_hsc($format, array_shifted($args));
}


function adds($string) {
	return addslashes($string);
}

function adds_deep($value) {
	return is_array($value) ? array_map('adds_deep', $value) : adds($value);
}

function vsprintf_adds($format, $args) {
	return vsprintf($format, adds_deep($args));
}

function sprintf_adds($format /* [, args [, ...]] */ ) {
	$args = func_get_args();
	return vsprintf_adds($format, array_shifted($args));
}


function adds_hsc($string) {
	return adds(hsc($string));
}

function adds_hsc_deep($value) {
	return is_array($value) ? array_map('adds_hsc_deep', $value) : adds_hsc($value);
}

function vsprintf_adds_hsc($format, $args) {
	return vsprintf($format, adds_hsc_deep($args));
}

function sprintf_adds_hsc($format /* [, args [, ...]] */ ) {
	$args = func_get_args();
	return vsprintf_adds_hsc($format, array_shifted($args));
}


function trim_deep($value) {
	return is_array($value) ? array_map('trim_deep', $value) : trim($value);
}

function vsprintf_trim($format, $args) {
	return vsprintf($format, trim_deep($args));
}

function sprintf_trim($format /* [, args [, ...]] */ ) {
	$args = func_get_args();
	return vsprintf_trim($format, array_shifted($args));
}


function func_deep($function, $value) {
	return is_array($value) ? array_map('func_deep', array_fill_0(count($value), $function), $value) : call_user_func($function, $value);
}

function vsprintf_func($function, $format, $args) {
	return vsprintf($format, func_deep($function, $args));
}

function sprintf_func($function, $format /* [, args [, ...]] */ ) {
	$args = func_get_args();
	return vsprintf_func($function, $format, array_shifted($args, 2));
}


function strlt($str1, $str2) {
	return strcmp($str1, $str2) < 0;
}

function streq($str1, $str2) {
	return strcmp($str1, $str2) === 0;
}

function strgt($str1, $str2) {
	return strcmp($str1, $str2) > 0;
}

function strcaselt($str1, $str2) {
	return strcasecmp($str1, $str2) < 0;
}

function strcaseeq($str1, $str2) {
	return strcasecmp($str1, $str2) === 0;
}

function strcasegt($str1, $str2) {
	return strcasecmp($str1, $str2) > 0;
}


function strlss($str1, $str2) {
	return strcmp($str1, $str2) < 0;
}

function strequ($str1, $str2) {
	return strcmp($str1, $str2) === 0;
}

function strgtr($str1, $str2) {
	return strcmp($str1, $str2) > 0;
}

function strcaselss($str1, $str2) {
	return strcasecmp($str1, $str2) < 0;
}

function strcaseequ($str1, $str2) {
	return strcasecmp($str1, $str2) === 0;
}

function strcasegtr($str1, $str2) {
	return strcasecmp($str1, $str2) > 0;
}


function strcat($str1, $str2 /* [, str3 [, ...]] */ ) {
	$args = func_get_args();
	return implode('', $args);
}


function str_replace_keys($search_replace, $subject) {
	return str_replace(array_keys($search_replace), $search_replace, $subject);
}

function substr_start_end($string, $start, $end) {
	return in_range_start_end(0, $start, $end, strlen($string)) ? substr($string, $start, $end - $start) : FALSE;
}

function substr_between($string, $start_str, $end_str, $offset = 0, $outer = FALSE) {
	$outer_start = strpos($string, $start_str, $offset);
	$inner_start = $outer_start + strlen($start_str);
	$inner_end = strpos($string, $end_str, $inner_start);
	$outer_end = $inner_end + strlen($end_str);
	return $outer ? substr_start_end($string, $outer_start, $outer_end) : substr_start_end($string, $inner_start, $inner_end);
}

function substr_between_rpos($string, $start_str, $end_str, $offset = 0, $outer = FALSE) {
	$outer_start = strpos($string, $start_str, $offset);
	$inner_start = $outer_start + strlen($start_str);
	$inner_end = strrpos($string, $end_str, $inner_start);
	$outer_end = $inner_end + strlen($end_str);
	return $outer ? substr_start_end($string, $outer_start, $outer_end) : substr_start_end($string, $inner_start, $inner_end);
}


?>